package com.client.action;

import com.admin.action.BannerAdminAction;
import com.admin.action.SoftwareExamineAction;
import com.admin.bean.GoodSoftwareType;
import com.client.service.IndexClientService;
import com.data.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 客户端首页数据
 *
 * @author:HingLo
 * @create 2017-09-27 11:20
 **/

@Controller
@RequestMapping("/indexClient/client/")
public class IndexClientAction {
    @Autowired
    private IndexClientService indexClientService;
    @Autowired
    private BannerAdminAction bannerAdminAction;
    @Autowired
    private SoftwareExamineAction softwareExamineAction;

    /**
     * 首页推广软件
     *
     * @return
     */
    @GetMapping("/indexSoftware")
    @ResponseBody
    public Map<String, Object> indexClientSoftwareAction(HttpServletRequest request) {
        //0 默认为首页推
        List<?> list = indexClientService.findindexClientService(0);
        return SoftwareMassageUtils.getSoftwareMassageUtils(list, request);
    }


    /**
     * Banner图id
     *
     * @return
     */
    @GetMapping("indexBanner")
    @ResponseBody
    public Map<String, Object> indexClientBannerAction(HttpServletRequest request) {
        List<?> list = indexClientService.findindexClientBannerService();
        String[] value = {"id", "rid", "typeid"};
        String iconUrl = JsonUtils.getBasePath(request) + "/indexClient/client/bannerIcon?id=";
        return SoftwareMassageUtils.getSoftwareMassageUtils(list, request, iconUrl, value);
    }

    /**
     * Banner图对应的软件链接
     *
     * @return
     */
    @RequestMapping("bannersoftware")
    @ResponseBody
    public Map<String, Object> indexBannerSoftwareAction(String rid, int typeId, HttpServletRequest request) {
        switch (typeId) {
            //精选软件
            case 0:
                List<?> list = indexClientService.indexBannerSoftwareService(rid);
                return SoftwareMassageUtils.getSoftwareMassageUtils(list, request);
            // 软件列表
            case 1:
                return this.goodChoiceSoftwareAction(rid, request);

            //HTML 界面
            default:
                list = indexClientService.indexBannerSoftwareService(rid);
                return SoftwareMassageUtils.getSoftwareMassageUtils(list, request);
        }

    }

    /***
     * 查看banner图
     * @param id
     * @param response
     */
    @GetMapping("bannerIcon")
    public void bannerIconAction(String id, HttpServletResponse response) {

        bannerAdminAction.lookBannerIconAction(id, response);

    }

    /**
     * 查看软件的图标
     *
     * @param response
     * @param id
     * @param type
     */
    @GetMapping("softwareIcon")
    public void softwareIconAction(HttpServletResponse response, String id, String type) {

        softwareExamineAction.readSoftwareImageAction(response, id, type);
    }


    /**
     * 热榜软件,抢先软件，
     *
     * @param param   hot榜还是抢先，0为热榜，其余为抢先
     * @param state   软件状态
     * @param type    软件类型，软件或者游戏
     * @param start
     * @param end
     * @param request
     * @return
     */
    @RequestMapping("hotSoftware")
    @ResponseBody
    public Map<String, Object> hotSoftwareAction(int param, int start, int end, int state, int type, HttpServletRequest request) {
//        state = 1;//默认我在线
//        type = 0;//默认是软件
        List<?> list;
        if (param == 0) {
            list = indexClientService.findHotSoftwareService(state, type, start, end);
        } else {
            list = indexClientService.findfristSoftwareService(state, type, start, end);
        }
        return SoftwareMassageUtils.getSoftwareMassageUtils(list, request);
    }

    /**
     * 进入到精选软件界面，查询出全部精选相关的数据(图片)
     *
     * @return
     */
    @GetMapping("inputGoodSoftware")
    @ResponseBody
    public Map<String, Object> inputGoodSoftwareAction(int type, HttpServletRequest request) {
        if (type == 0 || type == 1) {
            List<?> list = indexClientService.findGoodSoftwareService(type);
            String[] value = {"id", "name", "contents"};
            String iconUrl = JsonUtils.getBasePath(request) + "/indexClient/client/lookIconGoodSoftware?id=";
            return SoftwareMassageUtils.getSoftwareMassageUtils(list, request, iconUrl, value);
        }
        return JsonUtils.returnMassageUtils(false, null);

    }

    /**
     * 精选软件的图标
     *
     * @param id
     * @param response
     */
    @GetMapping("lookIconGoodSoftware")
    public void lookIconGoodSoftwareAction(String id, HttpServletResponse response) {

        if (CheckDataUtils.stringUtils(id)) {
            GoodSoftwareType goodSoftwareType = indexClientService.findGoodSoftwareService(id);
            byte[] inputStream = goodSoftwareType.getIcon();
            ImageLookUtils.responseImage(inputStream, response);
        }


    }


    /**
     * 精选软件查询
     *
     * @param request
     * @return
     */
    @GetMapping("goodChoiceSoftware")
    @ResponseBody
    public Map<String, Object> goodChoiceSoftwareAction(String id, HttpServletRequest request) {
        List<?> list = indexClientService.goodChoiceSoftwareService(id);
        return SoftwareMassageUtils.getSoftwareMassageUtils(list, request);

    }


    /**
     * 软件下载
     *
     * @param request
     * @param md5      软件的唯一标识符
     * @param response
     * @throws IOException
     */
    @RequestMapping("downloadsApk")
    public void softwareDownloadAction(String id, String md5, HttpServletRequest request, HttpServletResponse response) {

        if (CheckDataUtils.stringUtils(md5, id)) {
            md5 = md5.trim();
            String fileName = md5 + ".apk";
            //软件新的路径
            String filePath = FinalStringUtils.APKPATH + "apk/" + fileName;
            try {
                FileResumeUtils.downloadApk(fileName, filePath, request, response);
                //下载量+1
                indexClientService.addSoftwareDownloadsNum(id);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
